package com.hecore.lagou.mr.component;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Author: hecore
 * @Date: 2020/10/18 17:08
 * @Description: mapper
 *  前两个值为输入Key-value ，后两个为输出Key-value
 *   文本偏移量,一行文本内容  |   单词，统计值
 */

public class WcMapper extends Mapper<LongWritable, Text, Text, IntWritable>  {

    /**
     * 声明成员实例变量
     */
    Text k=new Text();
    IntWritable v=new IntWritable(1);

    /**
     * 文本中读取信息,信息是一行一行的
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
        // word_count处理一行数据的逻辑
        // 1.从Value获取一行数据
        String line = value.toString();
        // 2.切割
        String[] words=line.split(" ");
        // 3.输出 deal out logic
        for (String word:
             words) {
            k.set(word);
            context.write(k,v);
        }

    }

}
